using System;
using System.Threading;

namespace HelloWorld.Helper
{
    public class Worker
    {
        // This method is called when the thread is started.
        public void DoWork()
        {
            int a;
            while ((a = _num) > 0)
            {
                _num--;
                Console.WriteLine($"Worker thread: working {a}");

                Thread.Sleep(1000);
            }
            
            Console.WriteLine("Worker thread: terminating gracefully.");
        }

        public int Num 
        {
            set { _num = value; }
        }

        // Keyword volatile is used as a hint to the compiler that this data
        // member is accessed by multiple threads.
        private volatile int _num;
    }
}